package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	router := gin.Default()
	router.LoadHTMLGlob("src/03_gin/02_响应/templates/*")
	// 在golang中没有相对文件的路径，只有相对项目的路径
	router.StaticFile("img/img", "src/03_gin/02_响应/static/img.png")
	// 网页请求这个静态目录的前缀，第二个参数是一个目录，注意访问目录的前缀不要重复
	router.StaticFS("static", http.Dir("src/03_gin/02_响应/static/static"))
	router.GET("/", ReturnStr)
	router.GET("/json", ReturnJson)
	router.GET("/map", ReturnMap)
	router.GET("/xml", ReturnXML)
	router.GET("/yaml", ReturnYaml)
	router.GET("/html", ReturnHtml)
	router.GET("/redirect", Redirect)
	router.Run(":8080")
}

func ReturnStr(c *gin.Context) {
	c.String(http.StatusOK, "你好！")
}

type User struct {
	Name     string `json:"name"`
	Age      int    `json:"age"`
	Password string `json:"-"`
}

func ReturnJson(c *gin.Context) {
	c.JSON(http.StatusOK, User{Age: 25, Name: "泰隆talon", Password: "123456"})
}

func ReturnMap(c *gin.Context) {
	c.JSON(200, gin.H{"userName": "泰隆", "userAge": 23})
}

func ReturnXML(c *gin.Context) {
	c.XML(200, gin.H{"code": 200, "data": gin.H{"userName": "泰隆", "userAge": 23}, "msg": "ok"})
}

func ReturnYaml(c *gin.Context) {
	c.YAML(200, gin.H{"code": 200, "data": gin.H{"userName": "泰隆", "userAge": 23}, "msg": "ok"})
}

func ReturnHtml(c *gin.Context) {
	c.HTML(200, "index.html", gin.H{"userName": "泰隆", "userAge": 23})
}

func Redirect(c *gin.Context) {
	c.Redirect(http.StatusFound, "/")
}
